package tp.pr5.gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import tp.pr5.GameController;
import tp.pr5.Room;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ActionPanel extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7326945509914836931L;
	private JTextField _textField;
	private GameController _game;
	private JComboBox<String> _comboBox;
	private JButton _btnGo;
	private JButton _btnPick;
	private JButton _btnDrop;
	private JButton _btnUse;
	private JButton _btnQuit;
	private MapPanel _mapPanel;
	private PlayerPanel _playerPanel;

	public ActionPanel(GameController game, PlayerPanel playerpanel, MapPanel mappanel)
	{

		this._mapPanel = mappanel;
	//	this._currentMapCell = this._mapPanel.searchMapCell(5, 5);
		this._playerPanel = playerpanel;
		this._game = game;
	//	_playerPanel.update();
		//this._game.getCurrentRoom().enterRoom(this._currentMapCell);
		//this._currentMapCell.update();
		setBorder(new TitledBorder(null, "Actions", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		setToolTipText("Actions");
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]
		{ 119, 0, 0 };
		gridBagLayout.rowHeights = new int[]
		{ 38, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[]
		{ 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[]
		{ 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);

		// ---------GO

		_btnGo = new JButton("GO");
		_btnGo.addMouseListener(new MouseAdapter()
		{
			@Override public void mouseClicked(MouseEvent e)
			{
			//	Room cuartoAnterior = _game.getCurrentRoom();
				String command = transformCommand(_btnGo, _comboBox);
				_game.executeStep(command);
			
				
			}
		});

		GridBagConstraints gbc_btnGo = new GridBagConstraints();
		gbc_btnGo.fill = GridBagConstraints.BOTH;
		gbc_btnGo.insets = new Insets(0, 0, 5, 5);
		gbc_btnGo.gridx = 0;
		gbc_btnGo.gridy = 0;
		add(_btnGo, gbc_btnGo);

		// ---------FIN GO

		// ---------COMBOBOX

		_comboBox = new JComboBox<String>();
		_comboBox.setMaximumRowCount(4);
		_comboBox.setAlignmentX(Component.RIGHT_ALIGNMENT);
		_comboBox.setModel(new DefaultComboBoxModel<String>(new String[]
		{ "NORTH", "SOUTH", "EAST", "WEST" }));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.BOTH;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 0;
		add(_comboBox, gbc_comboBox);

		// ---------FIN COMBOBOX

		// ---------TEXTFIELD
		_textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 1;
		add(_textField, gbc_textField);
		_textField.setColumns(10);
		// ---------FIN TEXTFIELD

		// ---------PICK
		_btnPick = new JButton("PICK");
		_btnPick.addMouseListener(new MouseAdapter()
		{
			@Override public void mouseClicked(MouseEvent e)
			{
				String command = transformCommand(_btnPick, _textField);
				_game.executeStep(command);
				//TODO _playerPanel.update();
				_textField.setText("");
			//	_currentMapCell.update();
			}
		});
		GridBagConstraints gbc_btnPick = new GridBagConstraints();
		gbc_btnPick.fill = GridBagConstraints.BOTH;
		gbc_btnPick.insets = new Insets(0, 0, 5, 5);
		gbc_btnPick.gridx = 0;
		gbc_btnPick.gridy = 1;
		add(_btnPick, gbc_btnPick);
		// ---------FIN PICK

		// ---------DROP
		_btnDrop = new JButton("DROP");
		_btnDrop.addMouseListener(new MouseAdapter()
		{
			@Override public void mouseClicked(MouseEvent e)
			{
				String command = transformCommand(_btnDrop, _playerPanel);
				if (command==null) return;
				_game.executeStep(command);
				//_playerPanel.update();
			//	_currentMapCell.update();
			}
		});
		GridBagConstraints gbc_btnDrop = new GridBagConstraints();
		gbc_btnDrop.fill = GridBagConstraints.BOTH;
		gbc_btnDrop.insets = new Insets(0, 0, 5, 5);
		gbc_btnDrop.gridx = 0;
		gbc_btnDrop.gridy = 2;
		add(_btnDrop, gbc_btnDrop);
		// ---------FIN DROP

		// ---------USE
		_btnUse = new JButton("USE");
		_btnUse.addMouseListener(new MouseAdapter()
		{
			@Override public void mouseClicked(MouseEvent e)
			{
				String command = transformCommand(_btnUse, _playerPanel);
				String message=_game.executeStep(command);
				//TODO
			/*	if (message==null)
					JOptionPane.showMessageDialog(null, "Something changes...");
				_playerPanel.update();*/
			}
		});
		GridBagConstraints gbc_btnUse = new GridBagConstraints();
		gbc_btnUse.fill = GridBagConstraints.BOTH;
		gbc_btnUse.insets = new Insets(0, 0, 5, 0);
		gbc_btnUse.gridx = 1;
		gbc_btnUse.gridy = 2;
		add(_btnUse, gbc_btnUse);

		// ---------FIN USE

		// ---------QUIT
		_btnQuit = new JButton("QUIT");
		_btnQuit.addMouseListener(new MouseAdapter()
		{
			@Override public void mouseClicked(MouseEvent e)
			{
				String command = transformCommand(_btnQuit);
				_game.executeStep(command);// _playerPanel.update();
				
			}
		});
		GridBagConstraints gbc_btnQuit = new GridBagConstraints();
		gbc_btnQuit.fill = GridBagConstraints.BOTH;
		gbc_btnQuit.insets = new Insets(0, 0, 0, 5);
		gbc_btnQuit.gridx = 0;
		gbc_btnQuit.gridy = 3;
		add(_btnQuit, gbc_btnQuit);
		// ---------FIN QUIT
	}

	// funciones que sirven para enlazar los clicks con el juego.
	@SuppressWarnings("rawtypes") public String transformCommand(JButton button,
			JComboBox box)
	{
		String buttonAction = button.getText();
		String command = box.getModel().getSelectedItem().toString();
		return buttonAction + " " + command;
	}

	public String transformCommand(JButton button, String text)
	{
		String buttonAction = button.getText();
		return buttonAction + " " + text;
	}

	public String transformCommand(JButton button, PlayerPanel panel)
	{
		int numRow = panel.get_inventory().getSelectedRow();
		if (numRow == -1) {
			JOptionPane.showMessageDialog(this,
					"Por favor, seleccione algo en la tabla de la derecha",
					"No hay nada seleccionado", JOptionPane.ERROR_MESSAGE);
			return null;
		}
		String selected = panel.get_inventory().getModel().getValueAt(numRow, 0)
				.toString();
		return button.getText() + " " + selected;
	}

	public String transformCommand(JButton button, JTextField textField)
	{
		String buttonAction = button.getText();
		return buttonAction + " " + textField.getText();
	}

	public String transformCommand(JButton button)
	{
		String buttonAction = button.getText();
		return buttonAction;
	}

	
}